package it.rap.applications.movimenti.ricerche.widget;

import it.rap.application.logic.mappers.MovimentoLogic;
import it.rap.applications.persistence.entities.contoCorrente.ContoCorrente;
import it.rap.applications.persistence.entities.movimento.Movimento;
import it.rap.applications.persistence.entities.movimento.Saldo;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;

public class LittleReportComposite extends Composite  {

	private static final long serialVersionUID = 8468358182649864073L;
	private ContoCorrente 	cc;
	private final Composite	contentComp;
	private BigDecimal		totMov;
	private Label 			saldoLabel;
	private Label 			saldoLabelSaldoValue;
	private Label 			totaleAccreditiLabel;
	private Label 			totaleAccreditiLabelNumber;
	private Label			totaleAddebitiLabel;
	private Label			totaleAddebitiLabelNumber;
	
	private String			valuta = "EUR";
	
	private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
	
	public LittleReportComposite(Composite parent, ContoCorrente cc) {
		super(parent, SWT.NONE);
		setLayout( new RowLayout(SWT.HORIZONTAL) );
		this.cc = cc;
		contentComp = createComposite();
	}

	private Composite createComposite() {
		//Composite littleReportComposite = new Composite(this, SWT.NONE);
		try {
			 
			MovimentoLogic mLogic = new MovimentoLogic();
			totMov = (BigDecimal) mLogic.getTotMovimenti( cc );

			Movimento lastMov = mLogic.loadMovimentoMaxDataContabile( cc );
			
			Saldo saldo = mLogic.calcolaSaldo( cc );
		    
		    if ( lastMov != null ){
		    	saldoLabel = new Label(this, SWT.NONE);
		    	saldoLabel.setText( new StringBuffer("Saldo al ").append( lastMov.getDataContabile()==null?"":sdf.format( lastMov.getDataContabile() ) ).append(": ").toString() );
		    }
		    	    
		    saldoLabelSaldoValue = new Label(this, SWT.NONE);
		    if ( saldo.getSaldo() >= 0 ){
		    	saldoLabelSaldoValue.setForeground(new Color(Display.getCurrent(),0, 255, 0));
		    } else {
		    	saldoLabelSaldoValue.setForeground(new Color(Display.getCurrent(),255, 0, 0));
		    }
		    saldoLabelSaldoValue.setText( " "+saldo.getSaldo()+" "+ valuta );
			saldoLabelSaldoValue.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
		    
		    
		    totaleAccreditiLabel = new Label(this, SWT.NONE);
		    totaleAccreditiLabel.setText(new StringBuffer("Tot. Accrediti ").append(": ").toString() );
		    
		    totaleAccreditiLabelNumber = new Label(this, SWT.NONE);
		    if ( saldo.getTotaleAccrediti() >= 0 ){
		    	totaleAccreditiLabelNumber.setForeground(new Color(Display.getCurrent(),0, 255, 0));
		    } else {
		    	totaleAccreditiLabelNumber.setForeground(new Color(Display.getCurrent(),255, 0, 0));
		    }
		    totaleAccreditiLabelNumber.setText( " "+saldo.getTotaleAccrediti()+" "+ valuta );
		    totaleAccreditiLabelNumber.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
		    
		    totaleAddebitiLabel = new Label(this, SWT.NONE);
		    totaleAddebitiLabel.setText(new StringBuffer("Tot. Addebiti ").append(": ").toString() );
		    
		    totaleAddebitiLabelNumber = new Label(this, SWT.NONE);
		    if ( saldo.getTotaleAddebiti() < 0 ){
		    	totaleAddebitiLabelNumber.setForeground(new Color(Display.getCurrent(),0, 255, 0));
		    } else {
		    	totaleAddebitiLabelNumber.setForeground(new Color(Display.getCurrent(),255, 0, 0));
		    }
		    totaleAddebitiLabelNumber.setText( " "+saldo.getTotaleAddebiti()+" "+ valuta );
		    totaleAddebitiLabelNumber.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
		   
			
		} catch (Exception e2) {
			e2.printStackTrace();
		}
		return this;
	}
	
	public void refreshReport( ContoCorrente cc ){
		this.cc = cc;
		MovimentoLogic mLogic = new MovimentoLogic();
		this.totMov = (BigDecimal) mLogic.getTotMovimenti( cc );

		Movimento lastMov = mLogic.loadMovimentoMaxDataContabile( cc );
		
		Saldo saldo = mLogic.calcolaSaldo( cc );
		
	    if ( lastMov != null ){	    	
	    	saldoLabel.setText( new StringBuffer("Saldo al ").append( lastMov.getDataContabile()==null?"":sdf.format( lastMov.getDataContabile() ) ).append(": ").toString() );
	    } else {
	    	saldoLabel.setText( new StringBuffer("Saldo al ").append(" No data:").toString() );
	    }
	    
	    if ( saldo.getSaldo() >= 0 ){
	    	saldoLabelSaldoValue.setForeground(new Color(Display.getCurrent(),0, 255, 0));
	    } else {
	    	saldoLabelSaldoValue.setForeground(new Color(Display.getCurrent(),255, 0, 0));
	    }
	    saldoLabelSaldoValue.setText( " "+saldo.getSaldo()+" "+ valuta );
		
	    totaleAccreditiLabel.setText(new StringBuffer("Tot. Accrediti ").append(": ").toString() );
	    
	    if ( saldo.getTotaleAccrediti() >= 0 ){
	    	totaleAccreditiLabelNumber.setForeground(new Color(Display.getCurrent(),0, 255, 0));
	    } else {
	    	totaleAccreditiLabelNumber.setForeground(new Color(Display.getCurrent(),255, 0, 0));
	    }
	    totaleAccreditiLabelNumber.setText( " "+saldo.getTotaleAccrediti()+" "+ valuta );
	    
	    totaleAddebitiLabel.setText(new StringBuffer("Tot. Addebiti ").append(": ").toString() );
	    
	    if ( saldo.getTotaleAddebiti() < 0 ){
	    	totaleAddebitiLabelNumber.setForeground(new Color(Display.getCurrent(),0, 255, 0));
	    } else {
	    	totaleAddebitiLabelNumber.setForeground(new Color(Display.getCurrent(),255, 0, 0));
	    }
	    totaleAddebitiLabelNumber.setText( " "+saldo.getTotaleAddebiti()+" "+ valuta );
	}

	/**
	 * @return the cc
	 */
	public ContoCorrente getCc() {
		return cc;
	}

	/**
	 * @param cc the cc to set
	 */
	public void setCc(ContoCorrente cc) {
		this.cc = cc;
	}

	/**
	 * @return the totMov
	 */
	public BigDecimal getTotMov() {
		return totMov;
	}

	/**
	 * @param totMov the totMov to set
	 */
	public void setTotMov(BigDecimal totMov) {
		this.totMov = totMov;
	}

	/**
	 * @return the contentComp
	 */
	public Composite getContentComp() {
		return contentComp;
	}

}
